package com.ztf.pdf;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.io.FileUtil;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @projectName: javabase
 * @package: com.ztf.pdf
 * @className: HtmlToPdf
 * @author: kevin
 * @description: TODO
 * @date: 2022/6/22 0022 19:19
 * @version: 1.0
 */
public class HtmlToPdf {

    public static String text = "<div data-v-d6cfb952=\"\" class=\"pdf\" style=\"padding: 10px;\"><div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-align: center;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 22pt; font-weight: bold;\">潍坊银行金融驿站产品营销合作协议</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">甲方:</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">潍坊银行</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">联系人：</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">身份证号：</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">乙方：</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">王宝龙</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">身份证号：370784199611222316</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\"></span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">为促进甲方业务发展，乙方在潍坊银行金融驿站为甲方业务进行宣传和推广,潍坊银行金融驿站为基于微信公众号开发的金融产品营销系统，系统管理员可以在金融驿站上架金融产品，营销人员可以招募推广人，推广人协助将营销信息转发给客户，以此促进全行业务发展。为明确双方权利义务，经平等协商，本着自愿、平等、互惠互利的原则，签订本合作协议。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">第一条  合作事项</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">乙方接受甲方委托，在潍坊银行金融驿站为甲方业务联系、介绍客户。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">第二条  双方的权利和义务</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">（一）甲方的权利和义务</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">1.保证所提供业务的合法性。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">2.对乙方介绍客户的业务申请进行审查，对不符合条件的业务申请有权拒绝。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">3.有权根据业务推广情况，对乙方营销业务的积分奖励规则进行调整。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">（二）乙方的权利和义务</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">1.积极宣传推广甲方的业务，且在此过程中，不得与任何与办理信贷业务相关的中介机构联合，损害甲方利益。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">2.因履行本协议所获知的甲方任何经营或相关信息均属甲方商业秘密，乙方均不得向第三方泄露。保密期限包括在签订本协议前及协议解除后。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">3.获取在金融驿站有效营销业务的积分奖励；积分有效期及使用规则与潍坊银行积分商城相应要求与规则一致。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">第三条  协议期限、解除、变更与补充</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">（一）本协议长期有效。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">（二）本协议解除的条件</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">1.甲乙双方就解除协议协商一致。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">2.因不可抗力致使不能实现协议目的。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">3.招募乙方为推广人的甲方营销人员离职，本协议自动解除，但乙方仍可获取有效营销业务的积分奖励，直至业务结清。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">4.乙方违反本协议约定或存在违法违规行为给甲方造成损失（包括但不限于经济损失、声誉损失等）的，甲方有权解除本协议，并追究乙方的相关责任。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">（三）本协议如有未尽事宜，由甲乙双方协商一致，签订书面补充协议，补充协议与本协议具有同等法律效力。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">第四条  争议解决</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">甲乙双方对本协议有关条款的解释或履行发生争议时，应友好协商，协商不成的向甲方或甲方分支机构所在地的人民法院起诉。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; font-weight: bold;\">第五条  其他</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">（一）甲方可自行或授权其同一法人所辖支行或营业网点履行与本协议相关的甲方的权利义务（包括但不限于提起诉讼等）。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt; text-indent: 23.25pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">（二）本协议采用电子签署的方式，乙方勾选并点击确认，本协议即视为乙方自动签署，与亲笔签名具有同等法律效力。</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">甲方：</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">潍坊银行</span></div> <div data-v-d6cfb952=\"\" style=\"background-color: rgb(255, 255, 255); margin: 0pt 0pt 7.5pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">乙方（签字）：</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">王宝龙</span></div> <div data-v-d6cfb952=\"\" style=\"line-height: 12pt; margin: 0pt 0pt 10pt;\"><span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt;\">签订时间：</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: underline;\"></span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: none;\">年</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: underline;\"></span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: none;\">月</span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: underline;\"></span> <span data-v-d6cfb952=\"\" style=\"color: rgb(51, 51, 51); font-family:SimSun; font-size: 12pt; text-decoration: none;\">日</span></div></div>";
    public static void main(String[] args) {
        HtmlToPdf pdf = new HtmlToPdf();
        String pdfPath = "D:\\logs\\12313.pdf";
        pdf.htmlTextToPdf(pdfPath, FontFamilyEnum.SIMSUN);
    }
    /**
     * html文本转换成PDF
     * <p>
     * 由于 ITextpdf 对html 检测非常严格，html 头部必须声明
     * <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     * <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
     * <p>
     * 以及其他的都要加</>结束，所以一般的页面都将不支持 转换 pdf
     *
     * @param pdfPath        pdf生成路径
     * @param fontFamilyEnum {@link FontFamilyEnum}  中文字体信息
     */
    public static void htmlTextToPdf(String pdfPath, FontFamilyEnum fontFamilyEnum) {
        String ss1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
                " <html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\n";
        String ss2 = "<body>";
        String body = text;
        String ss3 = "</body></html>";
        String ss = ss1+ss2+body+ss3;
        htmlTextToPdf(ss, pdfPath, fontFamilyEnum);
    }


//    /**
//     * html文本转换成PDF
//     *
//     * @param file           html 文件
//     * @param pdfPath        pdf生成路径
//     * @param fontFamilyEnum {@link FontFamilyEnum}  中文字体信息
//     */
//    public static void htmlTextToPdf(File file, String pdfPath, FontFamilyEnum fontFamilyEnum) {
//        try {
//            htmlTextToPdf(FileUtil.toString(file), pdfPath, fontFamilyEnum);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


    /**
     * html文本转换成PDF
     *
     * @param htmlText       html字符串
     * @param pdfPath        pdf生成路径
     * @param fontFamilyEnum {@link FontFamilyEnum}  中文字体信息
     */
    public static void htmlTextToPdf(String htmlText, String pdfPath, FontFamilyEnum fontFamilyEnum) {
        System.out.println("html转pdf入参,htmltext: {} , pdfpath:{}");
        // 为了支持中文，检查html内容中是否包含 font-family:SimSun
        boolean fontFamilyFlag = htmlText.contains("font-family");
        boolean fontNameFlag = htmlText.contains(fontFamilyEnum.getFontName());
        if (!fontFamilyFlag || !fontNameFlag) {
            System.out.println("--请注意，当前转换的html未包含字体文件，中文可能转换失败--");
        }
        try {
            TimeInterval timer = DateUtil.timer();
            OutputStream outputStream = new FileOutputStream(pdfPath);
            ITextRenderer iTextRenderer = new ITextRenderer();
            //解决中文字体，需要单独下载字体
            //同时在前端样式中加入font-family:SimSun;
            ITextFontResolver fontResolver = iTextRenderer.getFontResolver();
            fontResolver.addFont("D:\\simsun.ttc",
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextRenderer.setDocumentFromString(htmlText);
            iTextRenderer.layout();
            iTextRenderer.createPDF(outputStream);
            outputStream.close();
        } catch (IOException  | com.lowagie.text.DocumentException e) {
            try {
                throw new Exception("PDF转换异常", e);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    /**
     * 字体枚举类
     */
    public enum FontFamilyEnum {

        SIMSUN("SimSun", "simsun.ttf");
        // 字体名称
        private String fontName;
        // 字体文件
        private String fontFileName;

        FontFamilyEnum(String fontName, String fontFileName) {
            this.fontName = fontName;
            this.fontFileName = fontFileName;
        }

        public String getFontName() {
            return fontName;
        }

        public String getFontFileName() {
            return fontFileName;
        }
    }

}
